using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using SharpCore.Data;
using SharpCore.Extensions;
using SharpCore.Utilities;

namespace MMS.DAO
{
	public class OrderForm_MaterialDAO
	{
		#region Fields

		private string connectionStringName;

		#endregion

		#region Constructors

		public OrderForm_MaterialDAO(string connectionStringName)
		{
			ValidationUtility.ValidateArgument("connectionStringName", connectionStringName);

			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the OrderForm_Material table.
		/// </summary>
		public void Insert(OrderForm_MaterialDTO orderForm_Material)
		{
			ValidationUtility.ValidateArgument("orderForm_Material", orderForm_Material);
            
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdOrderForm", orderForm_Material.IdOrderForm),
				new SqlParameter("@IdMaterial", orderForm_Material.IdMaterial),
				new SqlParameter("@Amount", orderForm_Material.Amount),
                new SqlParameter("@Remaining", orderForm_Material.Remaining),
				new SqlParameter("@PriceByDay", orderForm_Material.PriceByDay),
				new SqlParameter("@IdRepository", orderForm_Material.IdRepository),
				new SqlParameter("@RentDay", orderForm_Material.RentDay),
				new SqlParameter("@ReturnDay", orderForm_Material.ReturnDay),
				new SqlParameter("@IdType", orderForm_Material.IdType)
			};

			SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "sp_OrderForm_MaterialInsert", parameters);
		}

		/// <summary>
		/// Updates a record in the OrderForm_Material table.
		/// </summary>
		public void Update(OrderForm_MaterialDTO orderForm_Material)
		{
			ValidationUtility.ValidateArgument("orderForm_Material", orderForm_Material);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", orderForm_Material.Id),
				new SqlParameter("@IdOrderForm", orderForm_Material.IdOrderForm),
				new SqlParameter("@IdMaterial", orderForm_Material.IdMaterial),
				new SqlParameter("@Amount", orderForm_Material.Amount),
                new SqlParameter("@Remaining", orderForm_Material.Remaining),
				new SqlParameter("@PriceByDay", orderForm_Material.PriceByDay),
				new SqlParameter("@IdRepository", orderForm_Material.IdRepository),
				new SqlParameter("@RentDay", orderForm_Material.RentDay),
				new SqlParameter("@ReturnDay", orderForm_Material.ReturnDay),
				new SqlParameter("@IdType", orderForm_Material.IdType)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "sp_OrderForm_MaterialUpdate", parameters);
		}

        /// <summary>
        /// Adjust rent remaining amount of material in order
        /// </summary>
        /// <param name="orderFormId"></param>
        /// <param name="materialId"></param>
        /// <param name="amount"></param>
        public void AdjustRemainingAmount(string orderFormId, string materialId , int amount)
        {
            ValidationUtility.ValidateArgument("orderFormId", orderFormId);
            ValidationUtility.ValidateArgument("materialId", materialId);

            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@OrderFormId", orderFormId),
				new SqlParameter("@MaterialId", materialId),
				new SqlParameter("@Amount", amount)
			};

            SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "sp_OrderForm_MaterialAdjustRemainingAmount", parameters);
        }

		/// <summary>
		/// Deletes a record from the sp_OrderForm_Material table by its primary key.
		/// </summary>
		public void Delete(int id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", id)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "sp_OrderForm_MaterialDelete", parameters);
		}

		/// <summary>
		/// Deletes a record from the OrderForm_Material table by a foreign key.
		/// </summary>
		public void DeleteAllByIdMaterial(string idMaterial)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdMaterial", idMaterial)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "sp_OrderForm_MaterialDeleteAllByIdMaterial", parameters);
		}

		/// <summary>
		/// Deletes a record from the OrderForm_Material table by a foreign key.
		/// </summary>
		public void DeleteAllByIdOrderForm(string idOrderForm)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdOrderForm", idOrderForm)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "sp_OrderForm_MaterialDeleteAllByIdOrderForm", parameters);
		}

		/// <summary>
		/// Deletes a record from the OrderForm_Material table by a foreign key.
		/// </summary>
		public void DeleteAllByIdType(int idType)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdType", idType)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "sp_OrderForm_MaterialDeleteAllByIdType", parameters);
		}

		/// <summary>
		/// Selects a single record from the OrderForm_Material table.
		/// </summary>
		public OrderForm_MaterialDTO Select(int id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", id)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "sp_OrderForm_MaterialSelect", parameters))
			{
				if (dataReader.Read())
				{
					return MapDataReader(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects a single record from the OrderForm_Material table.
		/// </summary>
		public string SelectJson(int id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", id)
			};

			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "sp_OrderForm_MaterialSelect", parameters);
		}

		/// <summary>
		/// Selects all records from the OrderForm_Material table.
		/// </summary>
		public List<OrderForm_MaterialDTO> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "sp_OrderForm_MaterialSelectAll"))
			{
				List<OrderForm_MaterialDTO> orderForm_MaterialDTOList = new List<OrderForm_MaterialDTO>();
				while (dataReader.Read())
				{
					OrderForm_MaterialDTO orderForm_MaterialDTO = MapDataReader(dataReader);
					orderForm_MaterialDTOList.Add(orderForm_MaterialDTO);
				}

				return orderForm_MaterialDTOList;
			}
		}

		/// <summary>
		/// Selects all records from the OrderForm_Material table.
		/// </summary>
		public string SelectAllJson()
		{
			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "sp_OrderForm_MaterialSelectAll");
		}

		/// <summary>
		/// Selects all records from the OrderForm_Material table by a foreign key.
		/// </summary>
		public List<OrderForm_MaterialDTO> SelectAllByIdMaterial(string idMaterial)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdMaterial", idMaterial)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "sp_OrderForm_MaterialSelectAllByIdMaterial", parameters))
			{
				List<OrderForm_MaterialDTO> orderForm_MaterialDTOList = new List<OrderForm_MaterialDTO>();
				while (dataReader.Read())
				{
					OrderForm_MaterialDTO orderForm_MaterialDTO = MapDataReader(dataReader);
					orderForm_MaterialDTOList.Add(orderForm_MaterialDTO);
				}

				return orderForm_MaterialDTOList;
			}
		}

		/// <summary>
		/// Selects all records from the OrderForm_Material table by a foreign key.
		/// </summary>
		public List<OrderForm_MaterialDTO> SelectAllByIdOrderForm(string idOrderForm)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdOrderForm", idOrderForm)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "sp_OrderForm_MaterialSelectAllByIdOrderForm", parameters))
			{
				List<OrderForm_MaterialDTO> orderForm_MaterialDTOList = new List<OrderForm_MaterialDTO>();
				while (dataReader.Read())
				{
					OrderForm_MaterialDTO orderForm_MaterialDTO = MapDataReader(dataReader);
					orderForm_MaterialDTOList.Add(orderForm_MaterialDTO);
				}

				return orderForm_MaterialDTOList;
			}
		}

		/// <summary>
		/// Selects all records from the OrderForm_Material table by a foreign key.
		/// </summary>
		public List<OrderForm_MaterialDTO> SelectAllByIdType(int idType)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdType", idType)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "sp_OrderForm_MaterialSelectAllByIdType", parameters))
			{
				List<OrderForm_MaterialDTO> orderForm_MaterialDTOList = new List<OrderForm_MaterialDTO>();
				while (dataReader.Read())
				{
					OrderForm_MaterialDTO orderForm_MaterialDTO = MapDataReader(dataReader);
					orderForm_MaterialDTOList.Add(orderForm_MaterialDTO);
				}

				return orderForm_MaterialDTOList;
			}
		}

		/// <summary>
		/// Selects all records from the OrderForm_Material table by a foreign key.
		/// </summary>
		public string SelectAllByIdMaterialJson(string idMaterial)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdMaterial", idMaterial)
			};

			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "sp_OrderForm_MaterialSelectAllByIdMaterial", parameters);
		}

		/// <summary>
		/// Selects all records from the OrderForm_Material table by a foreign key.
		/// </summary>
		public string SelectAllByIdOrderFormJson(string idOrderForm)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdOrderForm", idOrderForm)
			};

			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "sp_OrderForm_MaterialSelectAllByIdOrderForm", parameters);
		}

		/// <summary>
		/// Selects all records from the OrderForm_Material table by a foreign key.
		/// </summary>
		public string SelectAllByIdTypeJson(int idType)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdType", idType)
			};

			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "sp_OrderForm_MaterialSelectAllByIdType", parameters);
		}

		/// <summary>
		/// Creates a new instance of the OrderForm_MaterialDTO class and populates it with data from the specified SqlDataReader.
		/// </summary>
		private OrderForm_MaterialDTO MapDataReader(SqlDataReader dataReader)
		{
			OrderForm_MaterialDTO orderForm_MaterialDTO = new OrderForm_MaterialDTO();
			orderForm_MaterialDTO.Id = dataReader.GetInt32("Id", 0);
			orderForm_MaterialDTO.IdOrderForm = dataReader.GetString("IdOrderForm", String.Empty);
			orderForm_MaterialDTO.IdMaterial = dataReader.GetString("IdMaterial", String.Empty);
			orderForm_MaterialDTO.Amount = dataReader.GetInt32("Amount", 0);
            orderForm_MaterialDTO.Remaining = dataReader.GetInt32("Remaining", 0);
			orderForm_MaterialDTO.PriceByDay = dataReader.GetInt32("PriceByDay", 0);
			orderForm_MaterialDTO.IdRepository = dataReader.GetString("IdRepository", String.Empty);
			orderForm_MaterialDTO.RentDay = dataReader.GetDateTime("RentDay", new DateTime(0));
			orderForm_MaterialDTO.ReturnDay = dataReader.GetDateTime("ReturnDay", new DateTime(0));
			orderForm_MaterialDTO.IdType = dataReader.GetInt32("IdType", 0);
            orderForm_MaterialDTO.TypeName = dataReader.GetString("TypeName", String.Empty);

			return orderForm_MaterialDTO;
		}

		#endregion
	}
}
